package com.test.prolenders.dialog_prolenders;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.prolenders.R;
import com.test.prolenders.bean_prolenders.config_prolenders.AppShowCustomize_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.AccountList_Fr_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.BankAccount_Fr_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class SubmitAccountDialog_Fr_prolenders extends Dialog {

    private LinearLayout groupLayout;

    private final Runnable submitRunnable;

    private final String page;

    public SubmitAccountDialog_Fr_prolenders(@NonNull Context context, Runnable submitRunnable, String page) {
        super(context);
        this.submitRunnable = submitRunnable;
        this.page = page;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_submit_account_fr_prolenders);

        getWindow().setBackgroundDrawableResource(R.color.transparent_prolenders);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        groupLayout = findViewById(R.id.dsaf_layout_group);

        findViewById(R.id.dsaf_ib_close).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SUBMIT_CANCEL_CLICK","");
            dismiss();
        });

        findViewById(R.id.dsaf_btn_submit).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SUBMIT_CONFIRM_CLICK","");
            if (submitRunnable!=null) submitRunnable.run();
            dismiss();
        });

        refreshAccountList();

    }

    private void refreshAccountList(){
        groupLayout.removeAllViews();

        ArrayList<BankAccount_Fr_prolenders> accounts = AccountList_Fr_prolenders.getInstance().getBankAccountList();

        for (BankAccount_Fr_prolenders account:accounts){
            addAccountView(account);
        }

    }

    private void addAccountView(BankAccount_Fr_prolenders account){

        if (account == null) return;

        String desc = account.getWalletDesc();

        View accountView = View.inflate(getContext(), R.layout.item_account_fr_prolenders, null);
        ImageView logoIv = accountView.findViewById(R.id.iaf_iv_logo);
        TextView channelTv = accountView.findViewById(R.id.iaf_tv_channel);
        TextView mobileTv = accountView.findViewById(R.id.iaf_tv_mobile);
        ImageButton deleteIb = accountView.findViewById(R.id.iaf_ib_delete);

        deleteIb.setVisibility(View.GONE);

        mobileTv.setText(account.getWalletMobile());
        channelTv.setText(desc);

        groupLayout.addView(accountView);

        //获取图标
        if (AppShowCustomize_prolenders.getInstance().getChannelLogoByDesc(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo())) return;
                    if (!AppShowCustomize_prolenders.getInstance().hasAccountChannel()) return;
                    URL url = new URL(account.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        AppShowCustomize_prolenders.getInstance().setChannelLogoByDesc(desc,bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(AppShowCustomize_prolenders.getInstance().getChannelLogoByDesc(desc));

    }

}
